package com.ibeetl.cms.service;


import com.ibeetl.admin.core.service.CoreBaseService;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.PlatformException;
import com.ibeetl.cms.dao.CmsArticleDao;
import com.ibeetl.cms.entity.CmsArticle;
import com.ibeetl.cms.web.query.ArticleQuery;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * CoreArticle Service
 */

@Service
@Transactional
public class CmsArticleService extends CoreBaseService<CmsArticle> {

    @Autowired private CmsArticleDao articleDao;

    //@Cacheable(value = CorePlatformService.ARTICLE_QUERYBYCONDITION_CACHE)
    public PageQuery<CmsArticle> queryByCondition(PageQuery query){
        PageQuery ret =  articleDao.queryByCondition(query);
        queryListAfter(ret.getList());
        return ret;
    }

    /**
     * 此方法使用ArticleQuery作为参数，是因为使用PageQuery作为参数时无法用toString方法，导致无法使用缓存
     */
    @Cacheable(value = CorePlatformService.ARTICLE_QUERYBYCONDITION_CACHE)
    public PageQuery<CmsArticle> queryByArticleQuery(ArticleQuery condition,Long pageSize, Long pageNo) {
        PageQuery<CmsArticle> page = condition.getPageQuery();
        page.setPageSize(pageSize);
        page.setPageNumber(pageNo);
        PageQuery ret =  articleDao.queryByCondition(page);
        queryListAfter(ret.getList());
        return ret;
    }

    @CacheEvict(cacheNames = {CorePlatformService.ARTICLE_QUERYBYCONDITION_CACHE}, allEntries = true)
    public void batchDelCoreArticle(List<Long> ids){
        try {
            articleDao.batchDelCoreArticleByIds(ids);
        } catch (Exception e) {
            throw new PlatformException("批量删除CoreArticle失败", e);
        }
    }

    @Override
    @CacheEvict(cacheNames = {CorePlatformService.ARTICLE_QUERYBYCONDITION_CACHE}, allEntries = true)
    public int upsert(CmsArticle model) {
        return super.upsert(model);
    }
}